package com.itheima.controller;

import com.itheima.domain.Person;
import com.itheima.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping
public class PersonController {
    @Autowired
    private PersonService personService;

    @GetMapping( "/ryvue/{id}")
    public Person select( @PathVariable Integer id) {
        Person person=personService.getByID(id);
        System.out.println(person);
        return  person;
    }

    @GetMapping("/ryvues")
    public List<Person> selectAll() {
        List<Person> personList =personService.getAll();
        System.out.println(personList);
        return personList;
    }

    @PutMapping("/ryvue")
    public int update( @RequestBody Person person) {
        Person person1=new Person();
        person1.setId(person.getId());
        person1.setName(person.getName());
        person1.setAge(person.getAge());
        System.out.println(person1);
        return personService.update(person1);
    }

    @PostMapping("/ryvue")
    public int insert(@RequestBody Person person) {
        Person person2=new Person();
        person2.setId(person.getId());
        person2.setName(person.getName());
        person2.setAge(person.getAge());
        System.out.println(person2);
        return personService.insert(person2);
    }

    @DeleteMapping("/ryvue/{id}")
    public int delete( @PathVariable Integer id) {
        Person person3=personService.getByID(id);
        System.out.println(person3);
        return personService.delete(id);
    }

}
